package com.snow.config.amdin;

//import com.snow.config.amdin.UserRealm;
//import org.apache.log4j.Logger;
//import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
//import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.log4j.Logger;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

/**
 * @Description:
 * @author: snow
 * @Date: 2020-01-21
 **/
@Configuration
public class ShiroConfig {

    private static Logger logger = Logger.getLogger(ShiroConfig.class);
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("manager") DefaultWebSecurityManager manager){
        logger.info("Enter shiroFilterFactoryBean Method");
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(manager);
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("/admin/index", "user");
        map.put(("/admin"),"user");
        map.put("/admin/article","user");
        map.put("/admin/setting","user");
        map.put("/admin/category","user");
        map.put("/admin/links","user");
        map.put("/admin/attach","user");
        map.put("/admin/comments","user");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        shiroFilterFactoryBean.setLoginUrl("/admin/login");
        logger.info("Exit shiroFilterFactoryBean Method");

        return shiroFilterFactoryBean;
    }


    /**
     * manager
     * @param userRealm
     * @return
     */
    @Bean(name="manager")
    DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm,
                                                        @Qualifier("rememberManager") CookieRememberMeManager cookieManager){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        securityManager.setRememberMeManager(cookieManager);
        return securityManager;

    }

    /**
     * cookieManager 对象
     * @param cookie
     * @return
     */
    @Bean(name="rememberManager")
    public CookieRememberMeManager rememberManager(@Qualifier("rememberMeCookie")SimpleCookie cookie ){
        CookieRememberMeManager cookieManager = new CookieRememberMeManager();
        cookieManager.setCookie(cookie);
        cookieManager.setCipherKey(Base64.decode("2AvVhdsgUs0FSA3SDFAdag=="));
        logger.info("Debug====> Return CookieManager with Cookie" + cookie );
        return cookieManager;

    }

    /**
     * 获得cookie对象
     * @return
     */
    @Bean(name="rememberMeCookie")
    public SimpleCookie getRememberCookie(){
        /**
         * 参数对用html中checkbook的name属性
         */
        SimpleCookie cookie = new SimpleCookie("rememberMe");
        /**
         * 记住我cookie 有效期30天，单位秒
         */
        cookie.setMaxAge(259200);
        return cookie;
    }
    /**
     * realm
     * @return
     */
    @Bean
    public UserRealm userRealm(@Qualifier("matcher")HashedCredentialsMatcher matcher){
        UserRealm realm = new UserRealm();
        realm.setCredentialsMatcher(matcher);
        return realm;
    }

    /**
     * 密码比对器
     * @return
     */
    @Bean(name="matcher")
    HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("sha-256");
        matcher.setHashIterations(1024);
        matcher.setStoredCredentialsHexEncoded(false);
        return matcher;

    }
}
